<?php
include "../Service/positionsService.php";

if(isset($_GET["action"]))
    $action = $_GET["action"];
else
    $action = "show";
if($action == "show")
{
    $result = getPositions();
    $n = 0;
    while($row = mysql_fetch_array($result))
    {
        $position['id'] = $row["POSITION_ID"];
        $position['title'] = $row["POSITION_TITLE"];
        $position['role'] = $row["POSITION_ROLE"];
        $position['descript'] = $row["POSITION_DESCRIPTION"];
        $arrPosition[] = $position;
        $n++;
    }
}
else if($action == "added")
{
    $position['title'] = $_POST["title"];
    $position['role'] = $_POST["role"];
    $position['descript'] = $_POST["descript"];
    $result = insertPositions($position);
    echo "<script>window.location.href='../admin/mod.php?page=positions'</script>";
}
else if($action == "deleted")
{
    $id = $_GET['id'];
    $result = deletePosition($id);
    if(!$result)
        echo "<script>alert('Khong the thuc hien')</script>";    
    echo "<script>window.location.href='../admin/mod.php?page=positions'</script>";
}
else if ($action == "edit")
{
    $id = $_GET["id"];
    $result = getPositionsById($id);
    $position['id'] = $result["POSITION_ID"];
    $position['title'] = $result["POSITION_TITLE"];
    $position['role'] = $result["POSITION_ROLE"];
    $position['descript'] = $result["POSITION_DESCRIPTION"];
}
else if($action == "edited"){
    $position['id'] = $_GET["id"];
    $position['title'] = $_POST["title"];
    $position['role'] = $_POST["role"];
    $position['descript'] = $_POST["descript"];
    $result = editPosition($position);
    if(!$result)
        echo "<script>alert('Khong the thuc hien')</script>";    
    echo "<script>window.location.href='../admin/mod.php?page=positions'</script>";
}
function showPositions(){
    $result = getPositions();
    while($row = mysql_fetch_array($result))
    {
        $position['id'] = $row["POSITION_ID"];
        $position['title'] = $row["POSITION_TITLE"];
        $position['role'] = $row["POSITION_ROLE"];
        $position['descript'] = $row["POSITION_DESCRIPTION"];
        $arrPosition[] = $position;
    }
    return $arrPosition;
}

?>